﻿Imports System.Data
Partial Class DatosBasicos_Programas_Default
    Inherits PaginaComun
    Dim operacion As String
    Dim PK As String
    Protected Sub BtnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnGuardar.Click
        Dim obj As New Programas
        obj.COD_PROGRAMA = Me.TxtCod.Text
        obj.NOM_PROGRAMA = Me.TxtNom.Text
        obj.COD_FACULTAD = Me.CmbFacultad.SelectedValue
        obj.RESOLUCION = TxtRes.Text
        obj.SENA = Util.invN1_0(ChkSena.Checked)
        obj.NOMBRE_SENA = TxtNomSena.Text
        obj.CODIGO_PK1 = Me.Pk1
        Select Case HfOper.Value
            Case "Nuevo"
                Me.Msg.Text = obj.Insert()
            Case "Editar"
                Me.Msg.Text = obj.Update()
        End Select
        Me.GridView1.DataBind()
        Me.MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Me.HfOper.Value = "Nuevo"
        limpiar()
        habilitar(True)
        Me.MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim obj As New Programas
        Select Case e.CommandName
            Case "Editar"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Me.GridView1.SelectedIndex = index
                Dim dt As DataTable = obj.consultarPorCod(GridView1.DataKeys(index).Values(0).ToString(), GridView1.DataKeys(index).Values(1).ToString())
                If dt.Rows.Count > 0 Then
                    Me.TxtCod.Text = dt.Rows(0)("Cod_Programa").ToString
                    Me.TxtNom.Text = dt.Rows(0)("Nom_Programa").ToString
                    Me.CmbFacultad.SelectedValue = dt.Rows(0)("Cod_Facultad").ToString
                    TxtRes.Text = dt.Rows(0)("Resolucion").ToString
                    ChkSena.Checked = Util.N1_0(dt.Rows(0)("Sena").ToString)
                    'Me.HfPK.Value = dt.Rows(0)("Cod_Facultad").ToString
                    Me.Pk1 = dt.Rows(0)("ID").ToString
                    Me.HfOper.Value = "Editar"
                    habilitar(True)
                    Me.MultiView1.ActiveViewIndex = 0
                End If
            Case "Eliminar"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Me.GridView1.SelectedIndex = index
                Dim dt As DataTable = obj.consultarPorCod(GridView1.DataKeys(index).Values(0).ToString(), GridView1.DataKeys(index).Values(1).ToString())
                If dt.Rows.Count > 0 Then
                    Me.TxtCod.Text = dt.Rows(0)("Cod_Programa").ToString
                    Me.TxtNom.Text = dt.Rows(0)("Nom_Programa").ToString
                    Me.CmbFacultad.SelectedValue = dt.Rows(0)("Cod_Facultad").ToString
                    Me.HfPK.Value = dt.Rows(0)("Cod_Facultad").ToString
                    TxtRes.Text = dt.Rows(0)("Resolucion").ToString
                    ChkSena.Checked = Util.N1_0(dt.Rows(0)("Sena").ToString)
                    Me.Pk1 = dt.Rows(0)("Cod_Programa").ToString
                    Me.HfOper.Value = "Eliminar"
                    habilitar(False)
                    Me.MultiView1.ActiveViewIndex = 0
                End If
        End Select
    End Sub
    Sub habilitar(ByVal val As Boolean)
        Me.CmbFacultad.Enabled = val
        Me.TxtCod.Enabled = val
        Me.TxtNom.Enabled = val
        Me.BtnGuardar.Enabled = val
        Me.BtnEliminar.Enabled = Not val
    End Sub
    Sub limpiar()
        Me.TxtCod.Text = ""
        Me.TxtNom.Text = ""
        TxtRes.Text = ""
        ChkSena.Checked = False
    End Sub

    Protected Sub BtnEliminar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEliminar.Click
        Dim obj As New Programas
        obj.CODIGO_PK1 = Me.Pk1
        Me.Msg.Text = obj.Delete()
        GridView1.DataBind()
        Me.MultiView1.ActiveViewIndex = 1
    End Sub
End Class
